package com.xiaoyu.merchant.ui.fragment.main;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.xiaoyu.commonlib.utils.CommonFragmentPagerAdapter;
import com.xiaoyu.commonlib.utils.ViewPagerUtils;
import com.xiaoyu.merchant.R;
import com.xiaoyu.merchant.ui.activity.commodity.UploadActivity;
import com.xiaoyu.merchant.ui.fragment.commodity.PutawayFragment;
import com.xiaoyu.merchant.ui.fragment.commodity.SoldOutFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * @author jx
 * @date 创建时间 2018/12/28
 * @Description 商品管理
 */
public class CommodityFragment extends Fragment implements View.OnClickListener {
    private View mView;
    private LinearLayout functionBar;
    private View mLine;
    private ViewPager mViewPager;
    private ViewPagerUtils mPagerUtils;
    private List<Fragment> mList = new ArrayList<>();
    private CommonFragmentPagerAdapter mPageAdapter;
    private TextView mUploadStore;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_commodity, container, false);
            //绑定fragment
            ButterKnife.bind(this, mView);
            initView();
        }
        ViewGroup parent = (ViewGroup) mView.getParent();
        if (parent != null) {
            parent.removeView(mView);
        }
        return mView;

    }

    private void initView() {
        functionBar = mView.findViewById(R.id.fragment_commodity_linear_bar);
        mLine = mView.findViewById(R.id.fragment_commodity_bar_line);
        mViewPager = mView.findViewById(R.id.fragment_commodity_view_pager);
        mUploadStore = mView.findViewById(R.id.fragment_upload_store);
        mUploadStore.setOnClickListener(this);

        initPager();

        //上拉下拉刷新设置
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsHeader(context);
            }
        });
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });

        mViewPager.addOnPageChangeListener(pageChangeListener);
        for (int i = 0; i < functionBar.getChildCount(); i++) {
            functionBar.getChildAt(i).setOnClickListener(this);
        }

    }

    private void initPager() {
        mPagerUtils = new ViewPagerUtils(getActivity(), functionBar, mLine);
        mPagerUtils.initLine(55);
        mPagerUtils.setTextColor("#333333", "#0c93e9");
        mPagerUtils.selectItem(0);

        mList.clear();
        mList.add(new PutawayFragment());//已上架
        mList.add(new SoldOutFragment());//已下架

        mPageAdapter = new CommonFragmentPagerAdapter(getFragmentManager(), mList);
        mViewPager.setAdapter(mPageAdapter);
        mViewPager.setCurrentItem(0);
    }

    /**
     * viewPager切换监听
     */
    private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            mPagerUtils.scrollLineMove(position, positionOffset);
        }

        @Override
        public void onPageSelected(int position) {
            mPagerUtils.selectItem(position);
            mViewPager.setCurrentItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    @Override
    public void onClick(View view) {
        if (view == functionBar.getChildAt(0)) {//精选

            mPagerUtils.selectItem(0);
            mPagerUtils.scrollLineMove(0, 0);
            mViewPager.setCurrentItem(0);

        } else if (view == functionBar.getChildAt(1)) {//最新

            mPagerUtils.selectItem(1);
            mPagerUtils.scrollLineMove(1, 0);
            mViewPager.setCurrentItem(1);

        } else if (view == mUploadStore) {//上传商品
            startActivity(new Intent(getActivity(), UploadActivity.class));
        }

    }
}
